package model.tokens;

import java.util.*;

import model.semantics.AggregatorNode;
import model.semantics.LeftGroupNode;
import model.semantics.Node;

import model.parser.ParserException;

/**
 * Right parenthesis.
 */

public class RightGroupToken<T> extends Token<T> {

	public RightGroupToken(String label) {
		super(label);
	}

	@Override
	public void process(Stack<Token<T>> operatorStack, Stack<Node<T>> output) {
		shuntFunctionsToOutput(operatorStack, output);
		
		// Make new aggregator node out of everything.
		List<Node<T>> nodes = new ArrayList<Node<T>>();
		
		while(!(output.peek() instanceof LeftGroupNode<?>)) {
			nodes.add(0, output.pop());
		}
		// Remove left group node from output stack.
		output.pop();
		
		output.add(new AggregatorNode<T>(nodes));
		
		shuntFunctionsToOutput(operatorStack, output);
	}
}
